/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>
#include <test_utilities/test_base.h>
#include <mc/engine.h>
#include <mc/common.h>
#include <thread>
#include <chrono>
#include <atomic>
#include "../../../../../drivers/pcie_gpu_card/innosilicon/interface/gpu.h"
#include "../../../../../drivers/pcie_gpu_card/innosilicon/interface/memory.h"
#include "../../../../../drivers/pcie_gpu_card/innosilicon/interface/pcie_card.h"
#include "../../../../../drivers/pcie_gpu_card/innosilicon/interface/pcie_device.h"
#include "../../../../../drivers/pcie_gpu_card/innosilicon/interface/processor.h"

namespace drivers {
namespace pcie_gpu_card {
namespace innosilicon {
namespace interface_test {


class interface_test_service : public mc::engine::service {
public:
    interface_test_service(const std::string& name) : mc::engine::service(name) {}
};

class InterfaceTest : public mc::test::TestWithEngine {
protected:
    static void SetUpTestSuite() {
        TestWithEngine::SetUpTestSuite();
        m_test_service = mc::make_shared<interface_test_service>("bmc.kepler.test_pcie_gpu_card_interface");
        m_test_service->init();
        m_test_service->start();
    }

    static void TearDownTestSuite() {
        if (m_test_service) {
            m_test_service->stop();
            m_test_service.reset();
        }
        TestWithEngine::TearDownTestSuite();
    }

    void SetUp() override {
        TestWithEngine::SetUp();
    }

    void TearDown() override {
        TestWithEngine::TearDown();
    }

    static mc::shared_ptr<interface_test_service> m_test_service;
};

mc::shared_ptr<interface_test_service> InterfaceTest::m_test_service;


// ========================= Gpu interface 测试 =========================

TEST_F(InterfaceTest, Gpu_Constructor) {
    // 目标：验证 Gpu 接口可以正常构造
    dev::Gpu gpu;
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Gpu_Destructor) {
    // 目标：验证 Gpu 接口可以正常析构
    {
        dev::Gpu gpu;
    }
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Gpu_MultipleInstances) {
    // 目标：验证可以创建多个 Gpu 实例
    std::vector<mc::shared_ptr<dev::Gpu>> gpus;
    
    for (int i = 0; i < 3; ++i) {
        auto gpu = mc::make_shared<dev::Gpu>();
        gpus.push_back(gpu);
    }
    
    EXPECT_EQ(gpus.size(), 3);
}

// ========================= Memory interface 测试 =========================

TEST_F(InterfaceTest, Memory_Constructor) {
    // 目标：验证 Memory 接口可以正常构造
    dev::Memory memory;
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Memory_Destructor) {
    // 目标：验证 Memory 接口可以正常析构
    {
        dev::Memory memory;
    }
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Memory_MultipleInstances) {
    // 目标：验证可以创建多个 Memory 实例
    std::vector<mc::shared_ptr<dev::Memory>> memories;
    
    for (int i = 0; i < 3; ++i) {
        auto memory = mc::make_shared<dev::Memory>();
        memories.push_back(memory);
    }
    
    EXPECT_EQ(memories.size(), 3);
}

// ========================= PCIeCard interface 测试 =========================

TEST_F(InterfaceTest, PCIeCard_Constructor) {
    // 目标：验证 PCIeCard 接口可以正常构造
    dev::PCIeCard card;
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, PCIeCard_Destructor) {
    // 目标：验证 PCIeCard 接口可以正常析构
    {
        dev::PCIeCard card;
    }
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, PCIeCard_SystemId) {
    // 目标：验证 PCIeCard 接口的 SystemId 属性可以正常访问和设置
    dev::PCIeCard card;
    
    // 设置 SystemId
    card.SystemId = 1;
    EXPECT_EQ(card.SystemId, 1);
    
    // 修改 SystemId
    card.SystemId = 2;
    EXPECT_EQ(card.SystemId, 2);
}

TEST_F(InterfaceTest, PCIeCard_SystemId_BoundaryValues) {
    // 目标：验证 PCIeCard 接口的 SystemId 属性可以处理边界值
    dev::PCIeCard card;
    
    card.SystemId = 0;
    EXPECT_EQ(card.SystemId, 0);
    
    card.SystemId = 255;  // uint8_t 最大值
    EXPECT_EQ(card.SystemId, 255);
}

TEST_F(InterfaceTest, PCIeCard_MultipleInstances) {
    // 目标：验证可以创建多个 PCIeCard 实例
    std::vector<mc::shared_ptr<dev::PCIeCard>> cards;
    
    for (int i = 0; i < 3; ++i) {
        auto card = mc::make_shared<dev::PCIeCard>();
        card->SystemId = static_cast<uint8_t>(i);
        cards.push_back(card);
    }
    
    EXPECT_EQ(cards.size(), 3);
    EXPECT_EQ(cards[0]->SystemId, 0);
    EXPECT_EQ(cards[1]->SystemId, 1);
    EXPECT_EQ(cards[2]->SystemId, 2);
}

// ========================= Processor interface 测试 =========================

TEST_F(InterfaceTest, Processor_Constructor) {
    // 目标：验证 Processor 接口可以正常构造
    dev::Processor processor;
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Processor_Destructor) {
    // 目标：验证 Processor 接口可以正常析构
    {
        dev::Processor processor;
    }
    EXPECT_TRUE(true);
}

TEST_F(InterfaceTest, Processor_MultipleInstances) {
    // 目标：验证可以创建多个 Processor 实例
    std::vector<mc::shared_ptr<dev::Processor>> processors;
    
    for (int i = 0; i < 3; ++i) {
        auto processor = mc::make_shared<dev::Processor>();
        processors.push_back(processor);
    }
    
    EXPECT_EQ(processors.size(), 3);
}

} // namespace interface_test
} // namespace innosilicon
} // namespace pcie_gpu_card
} // namespace drivers

